//
//  StrechVC.m
//  Demo
//
//  Created by LG on 2018/12/7.
//  Copyright © 2018 LG. All rights reserved.
//

#import "StrechVC.h"

@interface StrechVC ()

@end

@implementation StrechVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *image = [UIImage imageNamed:@"PPao_exclusivebg2"];
    CGSize imgSize = image.size;
    UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imgSize.height/2.-3,imgSize.width/2.-3,imgSize.height/2.-3,imgSize.width/2.-3) resizingMode:UIImageResizingModeTile];
    UIImageView *imgView = [[UIImageView alloc]initWithImage:newImage];
    imgView.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20, 120.);
    [self.view addSubview:imgView];
    
    [[self class]drawDashLine:imgView lineLength:3 lineSpacing:3 lineColor:[UIColor redColor]];
}

+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:2.];
    [shapeLayer setLineJoin:kCALineJoinMiter];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    UIBezierPath *beizierPath = [UIBezierPath bezierPathWithRect:lineView.bounds];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:beizierPath.CGPath];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
